<?php

namespace app\api\controller;

use app\admin\model\exam\MemberOrderModel;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        list($names, $orders, $money) = $this->renyuan();
        $this->success('请求成功',['names'=>$names,'orders'=>$orders,'money'=>$money]);
    }
    public function renyuan($date=''){
        if ($date) {
            list($start, $end) = explode(' - ', $date);

            $starttime = strtotime($start);
            $endtime = strtotime($end);
        } else {
            $starttime = \fast\Date::unixtime('month', 0, 'start');
            $endtime = \fast\Date::unixtime('month', 0, 'end');
        }
        $names  = [];
        $orders = [];
        $money  = [];
        $data = \app\common\model\User::where(['is_teacher'=>1])->field('id,nickname')->select();
        foreach ($data as $k=>$v){
            $names[$k]          = $v['nickname'];
            $orders[$k]         = MemberOrderModel::where(['parent_id'=>$v['id'],'status'=>'1'])->where('createtime', 'between time', [$starttime, $endtime])->count();
            $money[$k]          = MemberOrderModel::where(['parent_id'=>$v['id'],'status'=>'1'])->where('createtime', 'between time', [$starttime, $endtime])->sum('amount');
        }
        return [$names, $orders, $money];
    }
}
